package cn.codewen.shop.servlet;

import cn.codewen.shop.pojo.Admin;
import cn.codewen.shop.service.AdminService;
import cn.codewen.shop.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/admin")
public class AdminServlet extends HttpServlet {
    //0. 创建adminService对象
    private AdminService adminService=new AdminServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1. 接收参数
        String m = request.getParameter("m");
        //2. 判断
        switch (m) {
            case "list":
                list(request, response);
                break;
            case "add":
                add(request, response);
                break;
            case "edit":
                edit(request, response);
                break;
            case "del":
                del(request, response);
                break;
            case "login":
                login(request,response);
                break;
            case "logout":
                logout(request,response);
                break;
        }

    }

    private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1. 接收参数
        //2. 处理数据
        //2.1 去除admin的Session
        request.getSession().removeAttribute("admin");
        //3. 转发或跳转
        //3.1 转发数据
        //3.2 跳转
        response.sendRedirect("/admin/admin?m=login");
    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //0. 判断提交方式是GET还是POST
        if (request.getMethod().equals("POST")) {
            //POST方式处理数据
            //1. 接收参数
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            //2. 处理数据
            //2.1 创建adminService对象
            //2.2 调用对应的checkAdmin方法验证账号密码
            Admin admin = adminService.checkAdmin(username, password);
            //3. 转发或跳转
            //3.1 转发数据
            //3.2 跳转
            if (admin!=null){
                //3.2.1 设置Session
                request.getSession().setAttribute("admin",admin);
                //3.2.2 成功跳转到后台首页
                response.sendRedirect("/admin/index.jsp");
            }else {
                request.setAttribute("errMsg","账号或密码错误");
                request.getRequestDispatcher("/admin/admin_login.jsp").forward(request,response);
            }

        } else {
            //GET方式做展示

            //1. 接收参数
            //2. 处理数据
            //3. 转发或跳转
            request.getRequestDispatcher("/admin/admin_login.jsp").forward(request,response);

        }


    }

    private void del(HttpServletRequest request, HttpServletResponse response) {
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) {
    }

    private void add(HttpServletRequest request, HttpServletResponse response) {
    }

    private void list(HttpServletRequest request, HttpServletResponse response) {
    }
}
